import cefsharpAdapter from '../cefsharp'

// === Edge Tab相关处理函数 ===
export async function handleEdgeRequests(method, url, data, params) {
    console.log('🔍 [API] handleEdgeRequests:', { method, url, data, params })

    // GET请求处理
    if (method === 'get') {
        // 获取设备树数据
        if (/\/edge\/mqtt-format-json\/device-tree/.test(url)) {
            console.log('🔍 [API] CefSharp获取设备树数据:', params)
            const param = params?.param || ''
            return await cefsharpAdapter.callDbMethod('GetMqttFormatJsonDeviceTree', param)
        }

        // 获取mqtt-format-json设备变量JSON数据
        if (/\/edge\/mqtt-format-json\/devices\/[^/]+\/variables/.test(url)) {
            const deviceId = url.match(/\/edge\/mqtt-format-json\/devices\/([^/]+)\/variables/)?.[1]
            console.log('🔍 [API] CefSharp获取设备变量JSON数据:', deviceId)
            return await cefsharpAdapter.callDbMethod('GetMqttFormatJsonDevicesVarJson', deviceId)
        }

        // 获取mqtt-format-json所有配置
        if (/\/edge\/mqtt-format-json\/all/.test(url)) {
            console.log('🔍 [API] CefSharp获取mqtt-format-json所有配置')
            return await cefsharpAdapter.callDbMethod('GetAllMqttFormatJsons')
        }

        // 获取lua脚本
        if (/\/edge\/lua\/get/.test(url)) {
            console.log('🔍 [API] CefSharp获取lua脚本:', params)
            return await cefsharpAdapter.callDbMethod('GetConfig', params.key)
        }
    }

    // POST/PUT请求处理
    if (['post', 'put'].includes(method)) {
        return await handleEdgeModifyRequests(method, url, data)
    }
    return null
}

// === Edge修改请求处理 ===
export async function handleEdgeModifyRequests(method, url, data) {
    console.log('🔍 [API] handleEdgeModifyRequests:', { method, url, data })

    // MqttFormatJson相关操作
    if (/\/edge\/mqtt-format-json/.test(url)) {
        return await handleEdgeMqttFormatJsonRequests(method, url, data)
    }

    // lua相关操作
    if (/\/edge\/lua\/run/.test(url)) {
        console.log('🔍 [API] CefSharp运行Lua脚本:', data)
        return await cefsharpAdapter.callNativeMethod('RunLuaScript', data)
    }

    if (/\/edge\/lua\/save/.test(url)) {
        console.log('🔍 [API] CefSharp保存Lua脚本:', data)
        return await cefsharpAdapter.callDbMethod('SaveConfig', data)
    }

    console.log('❌ [API] 没有匹配到任何Edge操作类型')
    return null
}

// === Edge Tab请求处理 ===
export async function handleEdgeMqttFormatJsonRequests(method, url, data) {
    // 添加MqttFormatJson
    if (method === 'post' && /\/edge\/mqtt-format-json\/add/.test(url)) {
        console.log('🔍 [API] CefSharp添加MqttFormatJson:', data)
        // data是JSON字符串，需要解析
        let tabData
        try {
            tabData = typeof data === 'string' ? JSON.parse(data) : data
        } catch (error) {
            console.error('❌ [API] 解析Tab数据失败:', error)
            return { code: -1, message: '数据格式错误' }
        }
        return await cefsharpAdapter.callDbMethod('AddMqttFormatJson', JSON.stringify(tabData))
    }

    // 删除MqttFormatJson
    if (method === 'post' && /\/edge\/mqtt-format-json\/delete/.test(url)) {
        console.log('🔍 [API] CefSharp删除MqttFormatJson:', data)
        const { id } = data
        return await cefsharpAdapter.callDbMethod('DeleteMqttFormatJson', id)
    }

    // 保存MqttFormatJson
    if (method === 'post' && /\/edge\/mqtt-format-json\/save/.test(url)) {
        console.log('🔍 [API] CefSharp保存MqttFormatJson:', data)
        const { configData } = data
        return await cefsharpAdapter.callDbMethod('UpdateMqttFormatJson', configData)
    }
    return null
}